const app = getApp();
var util = require('../../utils/util.js');
var common = require('../../utils/common.js');
var url = require("../../utils/url.js");
var that;
var entity;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomType: '',
    info: '',
    img: '',
    night: 1,
    name: '',
    phone: '',
    remark: '',
    total: '',
    openid: '',
    initStartDate1: '',
    initEndDate1: '',
    initStartDate2: '',
    initEndDate2: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    //初始化日期
    var startDate = util.getTodayDate(new Date());
    var endDate = util.getTomorrowDate(new Date(), 1);
    var yearAfterDate1 = util.getTomorrowDate(new Date(), 365);
    var yearAfterDate2 = util.getTomorrowDate(new Date(endDate), 365);
    entity = JSON.parse(options.json);
    that.setData({
      initStartDate1: startDate,
      initEndDate1: yearAfterDate1,
      initStartDate2: endDate,
      initEndDate2: yearAfterDate2,
      startDate: app.globalData.inDate,
      endDate: app.globalData.outDate,
      roomType: entity.roomType,
      info: entity.info,
      img: entity.img,
      night: util.getDiffDays(app.globalData.inDate, app.globalData.outDate),
      total: util.getDiffDays(app.globalData.inDate, app.globalData.outDate) * entity.price,
    });
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        that.setData({
          openid: res.data,
        });
      },
    })
  },

  /**
   * 入住时间
   */
  bindStartDateChange: function(e) {
    that.setData({
      startDate: e.detail.value,
    })
    that.setData({
      night: util.getDiffDays(that.data.startDate, that.data.endDate),
    })
    console.log(that.data.night)
    if (that.data.night < 0) {
      that.setData({
        endDate: util.getTomorrowDate(new Date(that.data.startDate), 1),
        initStartDate2: util.getTomorrowDate(new Date(that.data.startDate), 1),
        initEndDate2: util.getTomorrowDate(new Date(that.data.initStartDate2), 365),
      })
    }
    that.setData({
      night: util.getDiffDays(that.data.startDate, that.data.endDate),
    })
  },

  bindEndDateChange: function(e) {
    that.setData({
      endDate: e.detail.value
    })
    that.setData({
      night: util.getDiffDays(that.data.startDate, that.data.endDate),
    })
  },

  inputName: function(e) {
    that.setData({
      name: e.detail.value
    });
  },

  inputPhone: function(e) {
    that.setData({
      phone: e.detail.value
    });
  },

  inputRemark: function(e) {
    that.setData({
      remark: e.detail.value
    });
  },

  getPhoneNumber: function(e) {
    wx.getStorage({
      key: 'code',
      success: function(res) {
        if (res.data) {
          console.log(e);
        }
      },
    })
  },

  /**
   * 确认预定
   */
  bindSure: function(e) {
    if (that.data.name == '') {
      common.showTip('请输入您的姓名');
    } else if (that.data.phone == '') {
      common.showTip('请输入您的手机号');
    } else {
      common.showLoading();
      wx.request({
        url: url.ADD_ORDER,
        method: 'POST',
        data: {
          openid: that.data.openid,
          roomId: entity.id,
          name: that.data.name,
          phone: that.data.phone,
          inTime: that.data.startDate,
          outTime: that.data.endDate,
          remark: that.data.remark,
          price: entity.price,
          night: that.data.night,
        },
        success: function(res) {
          common.closeLoading();
          if (res.data.code == 0) {
            wx.showToast({
              title: '预约成功',
              success: function(res) {
                wx.navigateBack({
                  delta: 2
                })
              }
            })
          }
        },
        fail: function(res) {
          common.closeLoading();
          common.showTip('预约失败，请重试');
        }
      })
    }

  },
})